home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / front.lha / front / src / input.lalr < prev    next >
Text File  |  1992-08-18  |  10KB  |  392 lines

  1. /* $Id: input.lalr,v 2.2 1992/08/07 15:15:11 grosch rel $ */
  2.  
  3. /* $Log: input.lalr,v $
  4.  * Revision 2.2  1992/08/07  15:15:11  grosch
  5.  * allow several scanner and parsers; extend module Errors
  6.  *
  7.  * Revision 2.1  1991/11/21  14:47:50  grosch
  8.  * new version of RCS on SPARC
  9.  *
  10.  * Revision 2.0  91/03/08  18:26:47  grosch
  11.  * turned tables into initialized arrays (in C)
  12.  * moved mapping tokens -> strings from Errors to Parser
  13.  * changed interface for source position
  14.  * 
  15.  * Revision 1.2  89/10/18  19:49:12  grosch
  16.  * renamed ScanTab and ParsTab to Scan.Tab and Pars.Tab because of PCTE
  17.  * 
  18.  * Revision 1.1  88/10/20  08:51:20  vielsack
  19.  * use tScanAttribute and tParsAttribute instead of tAttribute
  20.  * 
  21.  * Revision 1.0  88/10/04  14:27:31  vielsack
  22.  * Initial revision
  23.  * 
  24.  */
  25.  
  26. GLOBAL {
  27. FROM Actions    IMPORT PutAction, PutComment, tActionMode, ScannerName, ParserName;
  28. FROM Errors    IMPORT eError, eInteger, ErrorMessageI;
  29. FROM Lists    IMPORT Append, IsEmpty, Head, Tail, MakeList, tList;
  30. FROM Oper    IMPORT OperKind, MakePriority, CompletePriority, MakeOperator, MakeOperHeader;
  31. FROM Scanner    IMPORT GetToken, tScanAttribute, Attribute, ErrorAttribute;
  32. FROM Positions    IMPORT NoPosition;
  33. FROM Strings    IMPORT AssignEmpty, tString, ArrayToString;
  34. FROM StringMem    IMPORT PutString, tStringRef;
  35. FROM Idents    IMPORT NoIdent, MakeIdent;
  36. FROM SYSTEM    IMPORT ADR, ADDRESS;
  37. FROM TokenTab    IMPORT Terminal;
  38. FROM Tokens    IMPORT MakeGlobalHeader, MakeTokensHeader, MakeDeclaration, CompleteDeclarations;
  39. FROM Rules    IMPORT MakeRulesHeader, MakeLeafNode, MakeActionNode, MakeUnaryNode,
  40.             MakeBracketNode, MakeBinaryNode, MakePrioAlternativeNode,
  41.             MakeRule, Operation, NoExpression;
  42.  
  43. CONST
  44.   cEol = 12C;    (* eol character *)
  45.   eNumToBig = 9;
  46.  
  47. TYPE tParsAttribute = RECORD Scan: tScanAttribute; END;
  48.  
  49. VAR
  50.   String        ,
  51.   EndOfLineString    : tString;
  52.   EndOfLine        : ADDRESS;
  53. }
  54.  
  55. BEGIN {
  56.   AssignEmpty (EndOfLineString);
  57.   Strings.Append (EndOfLineString, cEol);
  58.   EndOfLine := ADDRESS (PutString (EndOfLineString));
  59. }
  60.  
  61. LOCAL { VAR ActionMode : tActionMode; }
  62.  
  63. TOKEN
  64.   '='        =  1
  65.   ':'        =  2
  66.   '.'        =  3
  67.   '|'        =  4
  68.   '*'        =  5
  69.   '+'        =  6
  70.   '||'        =  7
  71.   '('        =  8
  72.   ')'        =  9
  73.   '['        = 10
  74.   ']'        = 11
  75.   'EXPORT'    = 12
  76.   'GLOBAL'    = 13
  77.   'LOCAL'    = 14
  78.   'BEGIN'    = 15
  79.   'CLOSE'    = 16
  80.   'TOKEN'    = 17
  81.   'OPER'    = 18
  82.   'NONE'    = 19
  83.   'LEFT'    = 20
  84.   'RIGHT'    = 21
  85.   'RULE'    = 22
  86.   'PREC'    = 23
  87.   Comment    = 24 (* Schreibweise wie in MODULA-2                 *)
  88.   Number    = 25 (* vorzeichenlose ganze Zahl                    *)
  89.   Action    = 26 (* in '{' und '}' eingeschlossene Zeichenkette  *)
  90.   Identifier    = 27 (* letter (digit|letter)*                       *)
  91.   String    = 28 (* durch ' oder " begrenzte Zeichenkette        *)
  92.   'SCANNER'    = 29
  93.   'PARSER'    = 30
  94.  
  95. RULE
  96.  
  97.   Grammar    : CommentPart ScannerName ParserName Decl Tokens Oper Rules
  98. {
  99. MakeGlobalHeader ($1.Scan.Comm, $1.Scan.Position);
  100. }.
  101.  
  102.   ScannerName    :
  103. {
  104. ScannerName := NoIdent;
  105. }
  106.         | 'SCANNER'
  107. {
  108. ArrayToString ("Scanner", String); ScannerName := MakeIdent (String);
  109. }
  110.         | 'SCANNER' Identifier
  111. {
  112. ScannerName := $2.Scan.Sym;
  113. }.
  114.  
  115.   ParserName    :
  116. {
  117. ParserName := NoIdent;
  118. }
  119.         | 'PARSER'
  120. {
  121. ArrayToString ("Parser", String); ParserName  := MakeIdent (String);
  122. }
  123.         | 'PARSER' Identifier
  124. {
  125. ParserName := $2.Scan.Sym;
  126. }.
  127.  
  128.   Decl        : Decl 'EXPORT' {ActionMode := Export;} CommentPart Actions
  129. {
  130. PutComment(Export, $2.Scan.Position, $4.Scan.Comm, $4.Scan.Position);
  131. }
  132.         | Decl 'GLOBAL' {ActionMode := Global;} CommentPart Actions
  133. {
  134. PutComment(Global, $2.Scan.Position, $4.Scan.Comm, $4.Scan.Position);
  135. }
  136.         | Decl 'LOCAL' {ActionMode := Local;} CommentPart Actions
  137. {
  138. PutComment(Local, $2.Scan.Position, $4.Scan.Comm, $4.Scan.Position);
  139. }
  140.         | Decl 'BEGIN' {ActionMode := Begin;} CommentPart Actions
  141. {
  142. PutComment(Begin, $2.Scan.Position, $4.Scan.Comm, $4.Scan.Position);
  143. }
  144.         | Decl 'CLOSE' {ActionMode := Close;} CommentPart Actions
  145. {
  146. PutComment(Close, $2.Scan.Position, $4.Scan.Comm, $4.Scan.Position);
  147. }
  148.         | .
  149.  
  150.   Actions    : Action CommentPart
  151. {
  152. PutAction (ActionMode, $1.Scan.Act, $1.Scan.Position, $2.Scan.Comm, $2.Scan.Position);
  153. }
  154.         | .
  155.  
  156.   Tokens    : 'TOKEN' CommentPart Declarations
  157. {
  158. CompleteDeclarations;
  159. MakeTokensHeader( $1.Scan.Position, $2.Scan.Comm, $2.Scan.Position);
  160. }.
  161.  
  162.   Declarations    : Declarations Declaration
  163.         | Declaration .
  164.  
  165.   Declaration    : Terminal Coding CommentPart
  166. {
  167. MakeDeclaration( $1.Scan.Sym, $1.Scan.Position, $2.Scan.HasCoding, $2.Scan.Position,
  168.                  $2.Scan.CodValue, $2.Scan.CodNumbPos, $3.Scan.Comm, $3.Scan.Position);
  169. }.
  170.  
  171.   Coding    : '=' Number
  172. {
  173. $$.Scan.Mode := Scanner.Coding;
  174. IF ($2.Scan.Value > MAX(Terminal)) THEN
  175.   ErrorMessageI (eNumToBig, eError, $2.Scan.Position, eInteger, ADR ($2.Scan.Value));
  176.   $$.Scan.HasCoding    := FALSE;
  177.   $$.Scan.CodValue    := 0; (* Dummywert *)
  178.   $$.Scan.CodNumbPos    := NoPosition;
  179. ELSE
  180.   $$.Scan.HasCoding    := TRUE;
  181.   $$.Scan.CodValue    := $2.Scan.Value;
  182.   $$.Scan.CodNumbPos    := $2.Scan.Position;
  183. END;
  184. }
  185.         | 
  186. {
  187. $$.Scan.Mode        := Scanner.Coding;
  188. $$.Scan.HasCoding    := FALSE;
  189. $$.Scan.CodValue    := 0; (* Dummywert *)
  190. $$.Scan.CodNumbPos    := NoPosition;
  191. }.
  192.  
  193.   Oper        : 'OPER' CommentPart Priorities
  194. {
  195. MakeOperHeader($1.Scan.Position, $2.Scan.Comm, $2.Scan.Position);
  196. }
  197.         | .
  198.  
  199.   Priorities    : Priority Priorities 
  200.         | .
  201.  
  202.   Priority    : Associativity Operators CommentPart
  203. {
  204. CompletePriority($3.Scan.Comm, $3.Scan.Position);
  205. }.
  206.  
  207.   Associativity    : 'LEFT'  {MakePriority(Left, $1.Scan.Position);}
  208.         | 'RIGHT' {MakePriority(Right, $1.Scan.Position);}
  209.         | 'NONE'  {MakePriority(None, $1.Scan.Position);}.
  210.  
  211.   Operators    : Operator Operators
  212.         | Operator .
  213.  
  214.   Operator    : Terminal
  215. {
  216. MakeOperator($1.Scan.Sym, $1.Scan.Position);
  217. }.
  218.  
  219.   Terminal    : Identifier
  220. {
  221. $$.Scan.Mode        := Scanner.Symbol;
  222. $$.Scan.Sym        := $1.Scan.Sym;
  223. $$.Scan.Position    := $1.Scan.Position;
  224. }
  225.         | String
  226. {
  227. $$.Scan.Mode        := Scanner.Symbol;
  228. $$.Scan.Sym        := $1.Scan.Sym;
  229. $$.Scan.Position    := $1.Scan.Position;
  230. }.
  231.  
  232.   Rules        : 'RULE' CommentPart Rule *
  233. {
  234. MakeRulesHeader($1.Scan.Position, $2.Scan.Comm, $2.Scan.Position);
  235. }.
  236.  
  237.   Rule        : Identifier ':' RightSide '.' CommentPart
  238. {
  239. MakeRule($1.Scan.Sym,
  240.          $1.Scan.Position,
  241.          $2.Scan.Position,
  242.          $3.Scan.Expr,
  243.          $5.Scan.Comm,
  244.          $5.Scan.Position,
  245.          $4.Scan.Position,
  246.          $3.Scan.HasPrio,   (* Prioritaet der letzten Alternative *)
  247.          $3.Scan.Position,
  248.          $3.Scan.PriSym,
  249.          $3.Scan.PriSymPos);
  250. }.
  251.  
  252.   PrioPart    : 'PREC' Terminal
  253. {
  254. $$.Scan.Mode        := Scanner.PrioPart;
  255. $$.Scan.HasPrio        := TRUE;
  256. $$.Scan.Position    := $1.Scan.Position;
  257. $$.Scan.PriSym        := $2.Scan.Sym;
  258. $$.Scan.PriSymPos    := $2.Scan.Position;
  259. }
  260.         | 
  261. {
  262. $$.Scan.Mode        := Scanner.PrioPart;
  263. $$.Scan.HasPrio        := FALSE;
  264. $$.Scan.PriSymPos    := NoPosition;
  265. $$.Scan.PriSym        := 0;
  266. }.
  267.  
  268.   RightSide    : Expressions PrioPart '|' RightSide
  269. {
  270. $$.Scan        := $4.Scan;  (* PrioPart an Rule zurueckgeben *)
  271. $$.Scan.Mode    := Scanner.RightSide;
  272. $$.Scan.Expr    :=
  273. MakePrioAlternativeNode ($3.Scan.Position,
  274.                          $1.Scan.Expr,
  275.                          $4.Scan.Expr,
  276.                          $2.Scan.HasPrio,
  277.                          $2.Scan.Position,
  278.                          $2.Scan.PriSym,
  279.                          $2.Scan.PriSymPos);
  280. }
  281.         | Expressions PrioPart
  282. {
  283. $$.Scan        := $2.Scan;  (* Priopart an Rule zurueckgeben *)
  284. $$.Scan.Mode    := Scanner.RightSide;
  285. IF $1.Scan.Mode = Scanner.Empty THEN
  286.   $$.Scan.Expr    := NoExpression;
  287. ELSE
  288.   $$.Scan.Expr    := $1.Scan.Expr;
  289. END;
  290. }.
  291.  
  292.   Expressions    : Expression Expressions
  293. {
  294. IF $2.Scan.Mode # Scanner.Empty
  295. THEN (* Binary *)
  296.   $$.Scan.Mode    := Scanner.RightSide;
  297.   $$.Scan.Expr    := MakeBinaryNode(Sequence, NoPosition, $1.Scan.Expr, $2.Scan.Expr);
  298. ELSE (* Expressions ist leer - Expression ist weiterzureichen *)
  299.   $$.Scan    := $1.Scan;
  300. END; 
  301. }
  302.         | 
  303. {
  304. $$.Scan.Mode    := Scanner.Empty;  (* noetig damit kein 'leeres Bein' entsteht *)
  305. $$.Scan.Expr    := NoExpression;   (* noetig falls Expr weiterverwendet wird *)
  306. }.
  307.  
  308.   Expression    : Unit
  309. {
  310. $$.Scan.Mode    := Scanner.RightSide;
  311. $$.Scan.Expr    := $1.Scan.Expr;
  312. }
  313.         | Unit '*'
  314. {
  315. $$.Scan.Mode    := Scanner.RightSide;
  316. $$.Scan.Expr    := MakeUnaryNode(Star, $2.Scan.Position, $1.Scan.Expr);
  317. }
  318.         | Unit '+'
  319. {
  320. $$.Scan.Mode    := Scanner.RightSide;
  321. $$.Scan.Expr    := MakeUnaryNode(Plus, $2.Scan.Position, $1.Scan.Expr);
  322. }
  323.         | Unit '||' Unit
  324. {
  325. $$.Scan.Mode    := Scanner.RightSide;
  326. $$.Scan.Expr    := MakeBinaryNode(Separator, $2.Scan.Position , $1.Scan.Expr, $3.Scan.Expr);
  327. }.
  328.  
  329.   Unit        : '[' Alternative ']'
  330. {
  331. $$.Scan.Mode    := Scanner.RightSide;
  332. $$.Scan.Expr    := MakeBracketNode(Optional, $1.Scan.Position, $3.Scan.Position, $2.Scan.Expr);
  333. }
  334.         | '(' Alternative ')'
  335. {
  336. $$.Scan.Mode    := Scanner.RightSide;
  337. $$.Scan.Expr    := MakeBracketNode(Bracket, $1.Scan.Position, $3.Scan.Position, $2.Scan.Expr);
  338. }
  339.         | Identifier
  340. {
  341. $$.Scan.Mode    := Scanner.RightSide;
  342. $$.Scan.Expr    := MakeLeafNode($1.Scan.Sym, $1.Scan.Position);
  343. }
  344.         | String
  345. {
  346. $$.Scan.Mode    := Scanner.RightSide;
  347. $$.Scan.Expr    := MakeLeafNode($1.Scan.Sym, $1.Scan.Position);
  348. }
  349.         | Action
  350. {
  351. $$.Scan.Mode    := Scanner.RightSide;
  352. $$.Scan.Expr    := MakeActionNode ($1.Scan.Act, $1.Scan.Position);
  353. }.
  354.  
  355.   Alternative    : Expressions '|' Alternative
  356. {
  357. $$.Scan.Mode    := Scanner.RightSide;
  358. $$.Scan.Expr    := MakeBinaryNode (Alternative, $2.Scan.Position, $1.Scan.Expr, $3.Scan.Expr);
  359. }
  360.         | Expressions
  361. {
  362. $$.Scan.Mode    := Scanner.RightSide;
  363. IF $1.Scan.Mode = Scanner.Empty THEN
  364.   $$.Scan.Expr    := NoExpression;
  365. ELSE
  366.   $$.Scan.Expr    := $1.Scan.Expr;
  367. END;
  368. }.
  369.  
  370.   CommentPart    : CommentPart Comment
  371. {
  372. $$.Scan.Mode    := Scanner.Comment;
  373. IF IsEmpty ($1.Scan.Comm) THEN
  374.   $$.Scan.Position    := $2.Scan.Position;
  375.   $$.Scan.Comm        := $2.Scan.Comm;
  376. ELSE
  377.   Append ($1.Scan.Comm, EndOfLine);
  378.   WHILE NOT IsEmpty ($2.Scan.Comm) DO
  379.     Append ($1.Scan.Comm, Head($2.Scan.Comm));
  380.     Tail ($2.Scan.Comm);
  381.   END;
  382.   $$.Scan.Position    := $1.Scan.Position;
  383.   $$.Scan.Comm        := $1.Scan.Comm;
  384. END;
  385. }
  386.         | 
  387. {
  388. $$.Scan.Mode        := Scanner.Comment;
  389. $$.Scan.Position    := NoPosition;
  390. MakeList ($$.Scan.Comm);
  391. }.
  392.